﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Kendo.Mvc.Extensions;
using Kendo.Mvc.UI;
using System;
using System.Threading;
using LineOfBusiness.Core.Base;
using LineOfBusiness.Controls.Base;
using LineOfBusiness.Poco;
using LineOfBusiness.DataAccess;

namespace LineOfBusiness.AD.Controllers
{
    [PageAttribute(EntityChild = "AD_tblUserGroup", EntityMater = "AD_tblUserAccount")]
    public class GroupAccountController : LOBController
    {
        public GroupAccountController()
        {
            this.GridViewName = "grvtblUserAccount";
        }

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Add()
        {
            ViewBag.Edit = 0;
            AD_tblUserAccount GroupAccount = new AD_tblUserAccount();

            if (ViewData.ContainsKey(ViewDataKeys.Predicate))
                ViewData.Remove(ViewDataKeys.Predicate);

            if (ViewData.ContainsKey(ViewDataKeys.DataValue))
                ViewData.Remove(ViewDataKeys.DataValue);

            return View("Input", GroupAccount);
        }

        public ActionResult View(string UserGroupID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                AD_tblUserAccount UserAccount = dal.Repository.GetQuery<AD_tblUserAccount>().Where(o => o.UserGroupID == UserGroupID).FirstOrDefault();
                ViewBag.Edit = -1;
                return View("Input", UserAccount);
            }
        }

        public ActionResult Edit(string UserGroupID)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                AD_tblUserAccount UserAccount = dal.Repository.GetQuery<AD_tblUserAccount>().Where(o => o.UserGroupID == UserGroupID).FirstOrDefault();
                if (this.Request.QueryString.AllKeys.Contains("View"))
                    ViewBag.Edit = -1;
                else
                    ViewBag.Edit = 1;

                return View("Input", UserAccount);
            }
        }

        [HttpPost]
        public ActionResult Delete(AD_tblUserAccount GroupAccount)
        {
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    dal.Repository.Delete(GroupAccount);
                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

        [HttpPost]
        public ActionResult Save(object UserAccount)
        {
            string method = "Edit";
            string KeyOld = "";

            if (ViewData["Edit"].Equals("0"))
            {
                method = "Add";
            }
            else
            {
                KeyOld = ViewData["ID"] as string;
            }

            if (UserAccount != null)
            {
                foreach (var item in (UserAccount as AD_tblUserAccount).AD_tblUserGroup)
                {
                    item.UserID = (UserAccount as AD_tblUserAccount).UserGroupID;
                }
            }
            var SetupUserGroups = (UserAccount as AD_tblUserAccount);
            SetupUserGroups.FGroup = true;
            using (EFDALContainer dal = new EFDALContainer())
            {
                try
                {
                    if (method == "Add")
                    {
                        if (SetupUserGroups.AD_tblUserGroup != null && SetupUserGroups.AD_tblUserGroup.Count > 0)
                        {
                            foreach (var item in SetupUserGroups.AD_tblUserGroup)
                            {
                                dal.Repository.Add(item);
                            }
                        }
                        SetupUserGroups.AD_tblUserGroup = null;
                        dal.Repository.Add(SetupUserGroups);
                    }
                    else
                    {
                        var user = dal.Repository.GetQuery<AD_tblUserAccount>().Where(o => o.UserGroupID == SetupUserGroups.UserGroupID).FirstOrDefault();

                        if (KeyOld == SetupUserGroups.UserGroupID)
                        {
                            dal.Repository.Update(SetupUserGroups);
                            if (SetupUserGroups.AD_tblUserGroup != null)
                            {
                                if (SetupUserGroups.FGroup)
                                    dal.Repository.Delete<AD_tblUserGroup>(o => o.GroupID == SetupUserGroups.UserGroupID);
                                else
                                    dal.Repository.Delete<AD_tblUserGroup>(o => o.UserID == SetupUserGroups.UserGroupID);

                                foreach (var item in SetupUserGroups.AD_tblUserGroup)
                                {
                                    dal.Repository.Add(item);
                                }
                            }
                        }
                        else
                        {
                            if (SetupUserGroups.AD_tblUserGroup != null) dal.Repository.Delete<AD_tblUserAccount>(o => o.UserGroupID == KeyOld);
                            dal.Repository.Add(SetupUserGroups);
                        }
                    }

                    dal.UnitOfWork.SaveChanges();
                }
                catch (Exception exp)
                {
                    return Json(exp.Message);
                }
                return Json("0");
            }
        }

    }
}
